<?php
/**
 * 抽奖活动模型
 *
 *
 *
 *
 
 */
defined('InShopNC') or exit('Access Invalid!');
class lottery_giftModel extends Model{

    public function __construct() {
        parent::__construct('lottery_gift');
    }

	/**
     * 读取抢购列表
	 * @param array $condition 查询条件
	 * @param int $page 分页数
	 * @param string $order 排序
	 * @param string $field 所需字段
     * @return array 抢购列表
	 *
	 */
	public function getGiftList($condition, $page = null, $order = 'lottery_gift_id asc', $field = '*', $limit = 0) {
        return $this->field($field)->where($condition)->page($page)->order($order)->limit($limit)->select();
	}
	
	public function getGiftExtendList($condition, $page = null, $order = 'lottery_gift_id asc', $field = '*', $limit = 0) {
	    $gift_list = $this->getGiftList($condition, $page, $order, $field, $limit);
	    if(!empty($gift_list)) {
	        for ($i=0,$j=count($gift_list); $i<$j; $i++) {
	            $gift_list[$i] = $this->getExtendInfo($gift_list[$i]);
	        }
	    }
	    return $gift_list;
	}
	
	public function getExtendInfo($gift_info) {
	    if(!empty($gift_info)) {
	        $condition = array();
	        if($gift_info['type'] == 0) {
	            $condition['goods_id'] = $gift_info['goods_id'];
	            $goods = $this->table('goods')->where($condition)->find();
	            $gift_info['goods_name'] = $goods['goods_name'];
	            $gift_info['goods_url'] = thumb($goods, 32);
	        } else {
	            $condition['voucher_t_id'] = $gift_info['goods_id'];
	            $template = $this->table('voucher_template')->where($condition)->find();
	            $gift_info['goods_name'] = $template['voucher_t_title'];
	            if (!$template['voucher_t_customimg'] || !file_exists(BASE_UPLOAD_PATH.DS.ATTACH_VOUCHER.DS.$_SESSION['store_id'].DS.$template['voucher_t_customimg'])){
	                $gift_info['goods_url'] = UPLOAD_SITE_URL.DS.defaultGoodsImage(240);
	            }else{
	                $gift_info['goods_url'] = UPLOAD_SITE_URL.DS.ATTACH_VOUCHER.DS.$_SESSION['store_id'].DS.str_ireplace('.', '_small.', $template['voucher_t_customimg']);
	            }
	        }
	    }
	    return $gift_info;
	}
	
	/**
	 * 根据条件读取礼品信息
	 * @param array $condition 查询条件
	 * @return array 活动信息
	 *
	 */
	public function getGiftInfo($condition) {
	    $gift_info = $this->where($condition)->find();
	    if (empty($gift_info)) return array();
	    $gift_info = $this->getExtendInfo($gift_info);
	    return $gift_info;
	}
	
	public function updateGift($update,$gift_id){
	    if (empty($gift_id)) {
	        return true;
	    }
	    $condition['lottery_activity_id'] = $gift_id;    
	    $result = $this->table('lottery_gift')->where($condition)->update($update);
	    return $result;
	}
	
	/*
	 * 增加
	 * @param array $param
	 * @return bool
	 *
	 */
	public function addGift($param){
	    return $this->insert($param);
	}
	
	/*
	 * 删除
	 * @param array $param
	 * @return bool
	 *
	 */
	public function delGift($condition){
	    return $this->where($condition)->delete();
	}

}
